package bavaria.hightec.banking;

import java.util.Date;
import java.util.Iterator;

import bavaria.hightec.exceptions.OverCheckException;
import bavaria.hightec.time.TimeEmitter;

public abstract class Account {
	
	private int accountNumber;
	private Money balance;
	private Currency currency=Currency.EUR;
	private String accountHolder;
	private AccountingList<Accounting> list;
	private Date creation;
	

	
	public Account(Date creation,int accountNumber,String accountHolder){
		this.creation=creation;
		this.accountNumber=accountNumber;
		this.accountHolder=accountHolder;
		this.list=new AccountingList<Accounting>();
		this.balance = new Money(0,currency);
	}
	
	public int getAccountNumber(){
		return accountNumber;
	}
	
	public Date getCreationDate(){
		return creation;
	}
	
	public long getBalance(){
		return balance.getAmount();
	}
	
	public Currency getCurrency(){
		return currency;
	}
	
	public String getAccountHolder(){
		return accountHolder;
	}
	
	
	
	
	private void update(Money amount,String matter)throws OverCheckException{
			Date accounting = TimeEmitter.getInstance().getCalendar().getTime();
			Element<Accounting> accounting2 = new Element<Accounting>(accounting,amount,matter);
			balance.addition(amount.getAmount(), amount.getCurrency());
			list.add(accounting,amount, matter);
		
	}
	
	
	
	public void creditEntry(Money betrag,String matter) throws OverCheckException{
	
			update(betrag,matter);
			
	}
	
	public void debitEntry(Money amount,String matter) throws OverCheckException{
		long geld=-amount.getAmount();
		Money money = new Money(geld,amount.getCurrency());

		update(money,matter);
		
	}
	
	/**Verzinsungsmethode. Abstrakt 
	 * @throws OverCheckException **/
	
	public abstract void chargeInterest() throws OverCheckException;
	
	public String toString(){
		String string = "Kontoinhaber: " + accountHolder + ". Kontonummer: " + accountNumber + " Kontostand: " + getBalance();
		return string;
	}
		
	public void statement(){
		list.position = list.first;
		for(Iterator<Accounting> iter2 = list.iter(); iter2.hasNext(); list.print());
	}
	
	public void clear(){
		list.clear();
	}
	
	public void next(){
		list.next();
	}
	

}
